/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.model.statement;


import dex.compiler.model.base.Place;
import dex.compiler.model.expression.Expression;


/**
 * A for loop, like <code>for uint32 i = 0; i < 8; i++ { print(i) }</code>.
 */
public class ForStatement extends Statement {
	

	/**
	 * The statement that initalizes the loop controls.
	 */
	Statement initializer;
	

	/**
	 * The expression that tests if the loop should be exited.
	 */
	Expression test;
	
	
	/**
	 * The statement that modifies the loop variables.
	 */
	Statement modifier;
	
	
	/**
	 * The body of the loop.
	 */
	Statement body;

	
	/**
	 * Constructs a new for loop.
	 * 
	 * @param place  the place the for loop originated
	 * @param initializer  the initializer of the loop
	 * @param test  the test expression of the loop
	 * @param modifier  the modifier statement of the loop
	 * @param body  the body of the loop
	 */
	public ForStatement(Place place, Statement initializer, Expression test, Statement modifier, Statement body) {
		super(place);
		this.initializer = initializer;
		this.test = test;
		this.modifier = modifier;
		this.body = body;
	}


	@Override
	public String toString() {
		StringBuilder result = new StringBuilder();
		result.append("for ").append(initializer);
		result.append("; ").append(test);
		result.append("; ").append(modifier);
		result.append(body);
		return result.toString();
	}
	
	
	/**
	 * Returns the body of the loop.
	 * 
	 * @return the body of the loop
	 */
	public Statement getBody() {
		return body;
	}


	/**
	 * Returns the statement that initializes the loop.
	 * May be null if no initializer was specified.
	 * 
	 * @return the initializer
	 */
	public Statement getInitializer() {
		return initializer;
	}


	/**
	 * Returns the statement that modifies the loop controls.
	 * May be null if no modifer was specified.
	 * 
	 * @return the modifier
	 */
	public Statement getModifier() {
		return modifier;
	}


	/**
	 * Returns the expression used to test if the loop should continue.
	 * This expression should evaluate to a boolean.
	 * May be null if no test expression was specified.
	 * 
	 * @return  the test expression
	 */
	public Expression getTest() {
		return test;
	}


	@Override
	public boolean terminates() {
		if ((initializer != null) && (initializer.terminates())) {
			return true;
		}
		if ((modifier != null) && (modifier.terminates())) {
			return true;
		}
		return body.terminates();
	}

}
